package top.sky.log.impl;

import top.sky.log.config.LogConstant;
import org.apache.ibatis.logging.Log;
import org.slf4j.MDC;

import java.util.Objects;

/**
 * 自定义接口替换 {@link org.apache.ibatis.logging.stdout.StdOutImpl }SQL打印接口。
 *
 * @author shijin.liu
 * @see org.apache.ibatis.logging.stdout.StdOutImpl
 * @since 2023-07-01
 */
public class MybatisLogImpl implements Log {

    public MybatisLogImpl(String clazz) {
        // Do Nothing
        System.out.printf(" _, _ , _ __,  _, ___ _  _,   ___ __,  _,  _, __,   _,   _,  _,  _, _ _, _  _,\n" +
                " |\\/| \\ | |_) /_\\  |  | (_     |  |_) /_\\ / ` |_    |   / \\ / _ / _ | |\\ | / _\n" +
                " |  |  \\| |_) | |  |  | , )    |  | \\ | | \\ , |     | , \\ / \\ / \\ / | | \\| \\ /\n" +
                " ~  ~   ) ~   ~ ~  ~  ~  ~     ~  ~ ~ ~ ~  ~  ~~~   ~~~  ~   ~   ~  ~ ~  ~  ~ \n" +
                "       ~'                                                                     ");
    }

    @Override
    public boolean isDebugEnabled() {
        String transactionId = MDC.get(LogConstant.MDC_KEY_TRANSACTION_ID);
        if (Objects.isNull(transactionId)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isTraceEnabled() {
        return true;
    }

    @Override
    public void error(String s, Throwable e) {
        System.err.println(s);
        e.printStackTrace(System.err);
    }

    @Override
    public void error(String s) {
        System.err.println(s);
    }

    @Override
    public void debug(String s) {
        System.out.println(s);
    }

    @Override
    public void trace(String s) {
        System.out.println(s);
    }

    @Override
    public void warn(String s) {
        System.out.println(s);
    }
}
